﻿package  {
	import flash.geom.Point;
	
	public class King extends Piece
	{

		public var pieceName:String = "king";
		public var checked:Boolean;

		public function King(pieceColor:String, currentRow:int, currentColumn:int, firstMove:Boolean=false) 
		{
			this.firstMove = firstMove;
			this.checked = false;
			super(pieceColor, currentRow, currentColumn);
		}
		
		public function moves(board:Array, enPassantPawn:Object):Array
		{
			positions = new Array();
			currentBoard = board;
			var steps:int = 1;
			
			down(steps);
			up(steps);
			left(steps);
			right(steps);
			diagonalDown(steps);
			diagonalUp(steps);
			checkCastling(); //O famoso roque
			return positions;
		}
		
		public function checkCastling():void
		{
			if (!this.firstMove || this.checked)
				return;
			
			var rightHook = currentBoard[this.row][this.column + 3];
			var leftHook = currentBoard[this.row][this.column - 4]
			
			//Roque pela direita
			if (rightHook != null && rightHook.pieceName == "hook" && rightHook.firstMove && rightHook.pieceColor == this.pieceColor)
			{
				if (currentBoard[this.row][this.column + 1] == null && currentBoard[this.row][this.column + 2] == null)
				{
					positions.push(new Point(this.row,this.column + 2));
				}
			}
			
			//Roque pela esquerda
			if (leftHook != null && leftHook.pieceName == "hook" && leftHook.firstMove && leftHook.pieceColor == this.pieceColor)
			{
				if (currentBoard[this.row][this.column - 1] == null && currentBoard[this.row][this.column - 2] == null &&
					currentBoard[this.row][this.column - 3] == null)
				{
					positions.push(new Point(this.row,this.column - 2));
				}
			}
		}

	}
	
}
